﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

using Factonomy.AzureStorageUtility.WindowsApp.Resources;

namespace Factonomy.AzureStorageUtility.WindowsApp.UI.Forms
{
    public partial class AddNewDirectory : Form
    {
        public delegate void MyAddDirectoryDelegate(string path, string rootName);
        public event MyAddDirectoryDelegate AddDirectory;

        protected void OnAddDirectory(string path, string root)
        {
            if (AddDirectory != null)
            {
                AddDirectory(path, root);
            }
        }

        public AddNewDirectory()
        {
            InitializeComponent();

            // set the window icon
            this.Icon = ResourceManager.GetAppIcon();

            this.uiButtonAdd.Click += new EventHandler(uiButtonAdd_Click);
            this.uiButtonCancel.Click += new EventHandler(uiButtonCancel_Click);
            this.uiButtonSelectFile.Click += new EventHandler(uiButtonSelectFile_Click);
            this.uiTexboxRoot.Text = ConfigurationManager.AppSettings["rootpath"];
            this.uiTexboxRoot.TextChanged += new EventHandler(UpdateBlobNamePreview);
            this.uiTextboxFileName.TextChanged += new EventHandler(UpdateBlobNamePreview);

            this.uiLabelBlobName.Text = string.Empty;
        }

        private void UpdateBlobNamePreview(object sender, EventArgs e)
        {
            this.uiLabelBlobName.Text = this.uiTextboxFileName.Text.Replace(this.uiTexboxRoot.Text, "");
        }

        void uiButtonSelectFile_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                dialog.RootFolder = Environment.SpecialFolder.MyComputer;
                dialog.SelectedPath = this.uiTexboxRoot.Text;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.uiTextboxFileName.Text = dialog.SelectedPath;
                    this.UpdateBlobNamePreview(null, null);
                }
            }
        }

        void uiButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void uiButtonAdd_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to add " + this.uiLabelBlobName.Text + "?", "Confirm Add", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.OnAddDirectory(this.uiTextboxFileName.Text, this.uiTexboxRoot.Text);
                this.Close();
            }
        }
    }
}
